﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myOffice = Microsoft.Office.Interop;
using Tp.GUI.Dialog;
using TpTools;
using TpExtensions;
using TpData.Manager;
using TpContracts;

namespace Tp {
    public class AccCore {

        #region Members

        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "Tp.AccCore";

        #endregion

        #region Methods

        public void CreateWordEMail(string wordVorlage, string orgName, string suchbegriff) {
            bool lineBreak = false;
            Angebot an = new Angebot(orgName, suchbegriff, Extensions.tpWindowType.Korrepondenz);
            an.MyRecipient.Betreff = "";
            an.ShowDialog();

            if (an.Cancel) {
                return;
            }

            try {
                myOffice.Word.Application appWord = Office.GetWordApplication();
                appWord.Visible = true;
                //myOffice.Word.Document mDoc = Office.GetWordDocument(appWord, "", wordVorlage, log);
                myOffice.Word.Document mDoc = null;

                int repeats = 0;
                while (mDoc == null & repeats < 3) {
                    switch (an.Versand) {
                        case (int)GlobalVar.VersandArt.Mail:
                            mDoc = Office.GetWordDocument(appWord, "", @"H:\Vorlagen\Tk\To_maildb.dotx");
                            break;
                        case (int)GlobalVar.VersandArt.Brief:
                            mDoc = Office.GetWordDocument(appWord, "", @"H:\Vorlagen\Tk\To_briefdb.dotx");
                            lineBreak = true;
                            break;
                        case (int)GlobalVar.VersandArt.Fax:
                            mDoc = Office.GetWordDocument(appWord, "", @"H:\Vorlagen\Tk\To_faxdb.dotx");
                            break;
                    }
                    repeats++;
                }
                if (mDoc == null) {
                    LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "CreateWordEMail: mDoc = null", true);
                }


                if (Office.ExistingDocumentProperty(mDoc, "Subject")) {
                    mDoc.CustomDocumentProperties["Subject"].Delete();
                }
                mDoc.CustomDocumentProperties.Add("Subject", false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, an.MyRecipient.Betreff);

                if (Office.ExistingDocumentProperty(mDoc, "TpDocType")) {
                    mDoc.CustomDocumentProperties["TpDocType"].Delete();
                }
                mDoc.CustomDocumentProperties.Add("TpDocType", false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeNumber, Extensions.DocType.Mail);

                //mDoc.SaveAs2(FileName: fileName);
                Office.FillWordDocumentFields(appWord, mDoc, Extensions.DocType.Angebot, an.SelectedType, an.MyRecipient, null, null, an.Beilagen, an.MailCC, lineBreak);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "CreateWordEMail: " + ex.Message, true);
            }
        }

        public void OpenFormAngebot(string orgName, string suchbegriff) {
            Angebot an = new Angebot(orgName, suchbegriff, Extensions.tpWindowType.Mail);
            an.ShowDialog();
        }

        public void WordDokument(string wordVorlage) {

        }

        #endregion

    }
}
